package com.icefish.control;

import com.alibaba.fastjson.JSONObject;
import com.icefish.bean.User;
import com.icefish.bean.fastjsonParse.Meta;
import com.icefish.bean.fastjsonParse.getUserInfo.GetUserInfoResp;
import com.icefish.dao.UserDao;
import com.icefish.daoFactory.DaoFactory;
import com.icefish.utils.Init_Req_Resp_Config;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

@WebServlet("/getUserInfo")
public class getUserInfo extends HttpServlet {
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        Init_Req_Resp_Config.init(request, response);

        String user_id = request.getParameter("user_id");

        User user = null;
        // 1、调用UserDao的selectById方法，获取用户的所有信息
        UserDao ud = DaoFactory.getUserDaoInstance();
        try {
            user = ud.selectByID(Integer.parseInt(user_id));
        } catch (Exception e) {
            e.printStackTrace();
        }
        // 2、封装json数据，响应给客户端
        GetUserInfoResp guir = new GetUserInfoResp(user,new Meta("获取用户信息成功",200));

        JSONObject jsonObject = new JSONObject();
        // 转换成json
        String backToBrowser = jsonObject.toJSONString(guir);

        // 响应数据
        response.getWriter().write(backToBrowser);

    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        this.doPost(request, response);
    }
}
